import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12

Rectangle {
    color: "transparent"

    RowLayout {
        anchors.fill: parent
        spacing: 0

        // ---------- 左侧：按钮列 ----------
        Rectangle {
            width: 200
            Layout.fillHeight: true
            color: "#f8f9fa"
            border.color: "#dee2e6"
            radius: 8

            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 10
                anchors.rightMargin: 8
                anchors.bottomMargin: 316
                anchors.leftMargin: 0
                spacing: 1

                Text {
                    text: "系统服务"
                    font.pixelSize: 9
                    font.bold: true
                    color: "#2c3e50"
                    Layout.alignment: Qt.AlignHCenter
                }

                Rectangle {
                    height: 1
                    Layout.fillWidth: true
                    color: "#dee2e6"
                }

                Button {
                    text: "软件升级"
                    Layout.fillWidth: true
                    Layout.preferredHeight: 35
                    background: Rectangle {
                        color: mainStackView.currentItem == softwareUpgradePage ? "#3498db" : "#ecf0f1"
                        border.color: mainStackView.currentItem == softwareUpgradePage ? "#2980b9" : "#bdc3c7"
                        border.width: mainStackView.currentItem == softwareUpgradePage ? 2 : 1
                        radius: 6
                    }
                    contentItem: Text {
                        text: "软件升级"
                        color: mainStackView.currentItem == softwareUpgradePage ? "white" : "#2c3e50"
                        font.pixelSize: 14
                        font.bold: mainStackView.currentItem == softwareUpgradePage
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        mainStackView.replace(softwareUpgradePage)
                    }
                }

                Button {
                    text: "帮助与文档"
                    Layout.fillWidth: true
                    Layout.preferredHeight: 35
                    background: Rectangle {
                        color: mainStackView.currentItem == helpDocPage ? "#3498db" : "#ecf0f1"
                        border.color: mainStackView.currentItem == helpDocPage ? "#2980b9" : "#bdc3c7"
                        border.width: mainStackView.currentItem == helpDocPage ? 2 : 1
                        radius: 6
                    }
                    contentItem: Text {
                        text: "帮助与文档"
                        color: mainStackView.currentItem == helpDocPage ? "white" : "#2c3e50"
                        font.pixelSize: 13
                        font.bold: mainStackView.currentItem == helpDocPage
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        mainStackView.replace(helpDocPage)
                    }
                }

                Button {
                    text: "系统日志"
                    Layout.fillWidth: true
                    Layout.preferredHeight: 35
                    background: Rectangle {
                        color: mainStackView.currentItem == systemLogPage ? "#3498db" : "#ecf0f1"
                        border.color: mainStackView.currentItem == systemLogPage ? "#2980b9" : "#bdc3c7"
                        border.width: mainStackView.currentItem == systemLogPage ? 2 : 1
                        radius: 6
                    }
                    contentItem: Text {
                        text: "系统日志"
                        color: mainStackView.currentItem == systemLogPage ? "white" : "#2c3e50"
                        font.pixelSize: 13
                        font.bold: mainStackView.currentItem == systemLogPage
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        mainStackView.replace(systemLogPage)
                    }
                }
            }
        }

        // ---------- 右侧：内容显示区（StackView 切换页面） ----------
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.margins: 10
            color: "white"
            border.color: "#dee2e6"
            radius: 8

            StackView {
                id: mainStackView
                anchors.fill: parent
                anchors.margins: 15
                clip: true

                // ---------- 页面 1：软件升级 ----------
                Component {
                    id: softwareUpgradePage
                    Rectangle {
                        color: "transparent"

                        ColumnLayout {
                            anchors.fill: parent
                            spacing: 10

                            Text {
                                text: "软件升级信息"
                                font.pixelSize: 18
                                font.bold: true
                                color: "#2c3e50"
                                Layout.alignment: Qt.AlignHCenter
                            }

                            Rectangle {
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                color: "#f8f9fa"
                                border.color: "#e9ecef"
                                border.width: 1
                                radius: 6

                                Text {
                                    anchors.fill: parent
                                    anchors.margins: 15
                                    text: "当前版本：V1.0\n发布日期：2024-09-18\n\n主要更新内容：\n\n1. 增加了ModBus通信协议支持\n2. 修复了上位机心跳异常的问题\n3. 修复了PLC状态监控显示错误\n4. 优化了数据库查询效率\n5. 改进了用户界面响应速度\n6. 增强了系统稳定性"
                                    wrapMode: Text.Wrap
                                    font.pixelSize: 14
                                    color: "#2c3e50"
                                    lineHeight: 1.4
                                }
                            }

                            RowLayout {
                                Layout.alignment: Qt.AlignRight
                                spacing: 10

                                Button {
                                    text: "检查更新"
                                    Layout.preferredWidth: 100
                                    Layout.preferredHeight: 35
                                    background: Rectangle {
                                        color: "#17a2b8"
                                        radius: 6
                                    }
                                    contentItem: Text {
                                        text: "检查更新"
                                        color: "white"
                                        font.pixelSize: 13
                                        horizontalAlignment: Text.AlignHCenter
                                    }
                                    onClicked: console.log("检查软件更新")
                                }

                                Button {
                                    text: "查看详细日志"
                                    Layout.preferredWidth: 120
                                    Layout.preferredHeight: 35
                                    background: Rectangle {
                                        color: "#3498db"
                                        radius: 6
                                    }
                                    contentItem: Text {
                                        text: "查看详细日志"
                                        color: "white"
                                        font.pixelSize: 13
                                        horizontalAlignment: Text.AlignHCenter
                                    }
                                    onClicked: console.log("查看详细更新日志")
                                }
                            }
                        }
                    }
                }

                // ---------- 页面 2：帮助与文档 ----------
                Component {
                    id: helpDocPage
                    Rectangle {
                        color: "transparent"

                        ColumnLayout {
                            anchors.fill: parent
                            spacing: 10

                            Text {
                                text: "帮助与文档"
                                font.pixelSize: 18
                                font.bold: true
                                color: "#2c3e50"
                                Layout.alignment: Qt.AlignHCenter
                            }

                            ScrollView {
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                clip: true

                                Text {
                                    width: parent.width
                                    text: "甘蔗农机智能管控平台 V1.0 用户手册\n\n" +
                                          "1. 系统概述\n\n" +
                                          "1.1 产品简介\n" +
                                          "甘蔗农机智能管控平台 V1.0 是一款专为甘蔗农业机械设计的智能化管理控制系统，" +
                                          "提供远程监控、任务调度、数据分析等核心功能。\n\n" +
                                          "1.2 系统架构\n" +
                                          "本系统采用分布式架构设计，主要包含以下模块：\n" +
                                          "• 上位机监控系统\n" +
                                          "• PLC控制单元\n" +
                                          "• 数据库存储系统\n" +
                                          "• 通信接口模块\n\n" +
                                          "2. 主要功能\n\n" +
                                          "2.1 远程监控\n" +
                                          "实时监控设备运行状态、工作参数、位置信息等关键数据。\n\n" +
                                          "2.2 任务管理\n" +
                                          "支持任务创建、分配、执行监控和结果统计功能。\n\n" +
                                          "2.3 数据分析\n" +
                                          "提供设备性能分析、工作效率统计、故障预警等数据分析功能。\n\n" +
                                          "3. 操作指南\n\n" +
                                          "3.1 系统登录\n" +
                                          "使用管理员账号或操作员账号登录系统。\n\n" +
                                          "3.2 设备管理\n" +
                                          "在设备管理页面可以查看所有连接的设备状态。\n\n" +
                                          "3.3 任务调度\n" +
                                          "创建新的作业任务并分配给指定设备执行。\n\n" +
                                          "4. 故障排除\n\n" +
                                          "4.1 通信故障\n" +
                                          "检查网络连接和设备电源状态。\n\n" +
                                          "4.2 传感器异常\n" +
                                          "检查传感器连接线缆和电源供应。\n\n" +
                                          "5. 技术支持\n\n" +
                                          "如需技术支持，请联系系统管理员或查看系统日志获取详细错误信息。"
                                    wrapMode: Text.Wrap
                                    font.pixelSize: 14
                                    color: "#2c3e50"
                                    lineHeight: 1.4
                                }
                            }
                        }
                    }
                }

                // ---------- 页面 3：系统日志 ----------
                Component {
                    id: systemLogPage
                    Rectangle {
                        color: "transparent"

                        ColumnLayout {
                            anchors.fill: parent
                            spacing: 15

                            RowLayout {
                                Layout.fillWidth: true
                                spacing: 15

                                Text {
                                    text: "系统日志"
                                    font.pixelSize: 18
                                    font.bold: true
                                    color: "#2c3e50"
                                    Layout.fillWidth: true
                                }

                                Button {
                                    text: "刷新日志"
                                    Layout.preferredWidth: 100
                                    Layout.preferredHeight: 35
                                    background: Rectangle {
                                        color: "#28a745"
                                        radius: 6
                                    }
                                    contentItem: Text {
                                        text: "刷新日志"
                                        color: "white"
                                        font.pixelSize: 13
                                        horizontalAlignment: Text.AlignHCenter
                                    }
                                    onClicked: {
                                        logText.text = generateRandomLogs()
                                    }
                                }

                                Button {
                                    text: "导出日志"
                                    Layout.preferredWidth: 100
                                    Layout.preferredHeight: 35
                                    background: Rectangle {
                                        color: "#17a2b8"
                                        radius: 6
                                    }
                                    contentItem: Text {
                                        text: "导出日志"
                                        color: "white"
                                        font.pixelSize: 13
                                        horizontalAlignment: Text.AlignHCenter
                                    }
                                    onClicked: console.log("导出系统日志")
                                }
                            }

                            Rectangle {
                                Layout.fillWidth: true
                                Layout.fillHeight: true
                                color: "#f8f9fa"
                                border.color: "#e9ecef"
                                border.width: 1
                                radius: 6

                                ScrollView {
                                    anchors.fill: parent
                                    anchors.margins: 5
                                    clip: true

                                    Text {
                                        id: logText
                                        width: parent.width
                                        text: generateRandomLogs()
                                        wrapMode: Text.Wrap
                                        font.pixelSize: 12
                                        color: "#2c3e50"
                                        font.family: "Courier New"
                                        lineHeight: 1.3
                                    }
                                }
                            }
                        }
                    }
                }

                // 初始显示「软件升级」页面
                initialItem: softwareUpgradePage
            }
        }
    }

    // 生成随机系统日志的 JavaScript 函数
    function generateRandomLogs() {
        let logs = "系统日志信息：\n\n";
        // 模拟日志的「日期」「时间」「消息」
        const dates = ["2024-09-15", "2024-09-16", "2024-09-17", "2024-09-18", "2024-09-19"];
        const times = ["08:30:15", "09:15:33", "11:05:12", "14:20:45", "16:10:22", "17:45:18"];
        const messages = [
            "系统启动正常",
            "接收到设备连接请求",
            "数据库备份完成",
            "PLC通信状态正常",
            "上位机心跳包接收正常",
            "用户 admin 登录成功",
            "任务调度模块初始化完成",
            "传感器数据采集正常",
            "位置信息更新完成",
            "作业任务执行开始",
            "设备状态检查通过",
            "网络连接状态良好",
            "配置文件加载成功",
            "系统资源使用正常"
        ];

        // 生成 12 条随机日志
        for (let i = 0; i < 12; i++) {
            const date = dates[Math.floor(Math.random() * dates.length)];
            const time = times[Math.floor(Math.random() * times.length)];
            const msg = messages[Math.floor(Math.random() * messages.length)];
            const level = Math.random() > 0.8 ? "[ERROR]" : Math.random() > 0.6 ? "[WARN]" : "[INFO]";
            logs += `${date} ${time} ${level} - ${msg}\n`;
        }
        return logs;
    }
}

/*##^##
Designer {
    D{i:0;autoSize:true;formeditorZoom:0.9;height:480;width:640}
}
##^##*/
